package org.example.maven.controller;

import org.apache.ibatis.annotations.Delete;
import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.annotation.logAnnotation;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.department.list.AddDepartment;
import org.example.maven.pojo.department.list.DepartmentListTable;
import org.example.maven.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @GetMapping
    //获取所有部门信息
    public Result getAllDepartment(){
        return Result.success(departmentService.getAllDepartmentList());
    }

    @GetMapping("/getSpecieDepartment")
    @PreAuthorize("permitAll()")
    //获取特定的部门信息
    public Result getSpecieDepartment(@RequestParam(name = "deptName",required = false) String deptName,@RequestParam(name = "status",required = false) Integer status){

        return Result.success(departmentService.getSpecieDepartment(deptName,status));
    }

    @GetMapping("/getDepartmentSelect")
    //获取部门单选框的选项信息
    @PreAuthorize("permitAll()")
    public Result getDepartmentSelect(){
        return Result.success(departmentService.getDepartmentSelect());
    }

    @PostMapping
    //添加部门
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('department:orange')")
    @logAnnotation(
            type = "新增",
            detail = "新增部门{#addDepartment.deptName}"
    )
    public Result addDepartment(@RequestBody AddDepartment addDepartment){
        AddDepartment departmentListTable=departmentService.findDepartmentByName(addDepartment.getDeptName());
        AddDepartment departmentListTable1=departmentService.findDepartmentIsDeleted(addDepartment.getDeptName());
        if(departmentListTable==null){
            if(departmentListTable1==null){
                departmentService.addDepartment(addDepartment);
                return Result.success("新增部门成功");
            }else {
                departmentListTable1.setIsDeleted(0);
                departmentListTable1.setParentId(addDepartment.getParentId());
                departmentListTable1.setStatus(addDepartment.getStatus());
                departmentListTable1.setTime(addDepartment.getTime());
                departmentListTable1.setDeptName(addDepartment.getDeptName());
                departmentListTable1.setManagerId(addDepartment.getManagerId());
                System.out.println(departmentListTable1);
                departmentService.updateDeletedDepartment(departmentListTable1);
                return Result.success("新增部门成功");
            }
        }else {
            return Result.error("新增失败，部门已存在！");
        }
    }

    @GetMapping("/getDepartmentById")
    @PreAuthorize("permitAll()")
    //根据部门id获取部门
    public Result getDepartmentById(@RequestParam Integer id){
        return Result.success(departmentService.getDepartmentById(id));
    }

    @PutMapping
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('department:orange')")
    @logAnnotation(
            type = "更新",
            detail = "更新部门{#addDepartment.deptName}"
    )
    //更新部门信息
    public Result updateDepartmentById(@RequestBody AddDepartment addDepartment){
        departmentService.updateDepartmentById(addDepartment);
        return Result.success();
    }

    @DeleteMapping
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('department:orange')")
    @logAnnotation(
            type = "删除",
            detail = "删除部门"
    )
    //删除部门
    public Result deleteDepartment(@RequestParam("id")Integer id){
        departmentService.deleteDepartment(id);
        return Result.success();
    }
}
